Skip to content

tmpdir: write .origin sidecar to record session ownership - #14936

Open
jawauntb wants to merge 2 commits into
pytest-dev:mainfrom
jawauntb:jb/tmpdir-origin-sidecar
Open

tmpdir: write .origin sidecar to record session ownership#14936
jawauntb wants to merge 2 commits into
pytest-dev:mainfrom
jawauntb:jb/tmpdir-origin-sidecar

Conversation

@jawauntb

Copy link
Copy Markdown

Refs #14935 (part 1 of 3).

What

Each pytest-of-<user>/pytest-N/ session directory now contains a .origin
file next to .lock, recording:

  • rootpath: the absolute pytest rootpath
  • version: the pytest version that created it
  • pid: the PID of the creating process
  • host: the hostname of the creating machine

Example:

$ cat /tmp/pytest-of-me/pytest-2/.origin
rootpath=/Users/me/proj-a
version=9.2.0
pid=48211
host=laptop.local

Why

Right now nothing in the session directory records which rootdir produced it.
External cleanup tooling (workstation janitors, CI cleanup steps, editor
temp sweeps, ad-hoc du-and-rm passes) has to reconstruct ownership at
delete time from live process state (/proc walks, lsof). Getting that
wrong deletes live scratch; getting it conservative leaves disks full.

cat .origin closes that gap without any change to how pytest itself manages
retention.

Scope

This PR only adds the sidecar. It is intentionally additive and does not
change any existing behavior:

  • No public API changes to TempPathFactory, tmp_path, tmp_path_factory,
    or --basetemp.
  • No cleanup logic changes.
  • No retention or layout changes.
  • The _rootpath field on TempPathFactory gets a default of None, so
    external callers constructing the factory directly (as several existing
    tests do) are unaffected.

Two follow-ups are drafted in #14935 and will land as separate PRs so each
can be reviewed on its own merits:

  • Pid-liveness check in ensure_deletable (turns LOCK_TIMEOUT = 3 days
    into a fallback rather than the primary signal).
  • Per-rootdir retention layout (tmp_path_layout = "per-rootdir"), which
    is where the sidecar starts to earn its keep — retention becomes scoped
    per project instead of shared across every rootdir a user has ever run
    pytest against.

Safety

Writing the sidecar is best-effort — any OSError during the write is
swallowed. A read-only mount, permissions error, or full disk cannot
break a test run. Covered by TestOriginSidecar::test_origin_write_failure_does_not_break_run.

Tests

New tests in testing/test_tmpdir.py::TestOriginSidecar:

  • test_origin_written_next_to_basetemp — asserts the file exists with all
    four fields correctly populated.
  • test_origin_written_when_basetemp_given--basetemp also gets a
    sidecar so external tooling can attribute both cases uniformly.
  • test_origin_write_failure_does_not_break_run — proves the swallow path.

Existing test_tmpdir.py tests (61 passed, 1 skipped locally) plus
test_pytester.py and test_config.py (340 passed, 1 skipped, 2 xfailed
locally) confirm no regressions.

Changelog

changelog/14935.improvement.rst added, keyed to the tracking issue.

🤖 Generated with Claude Code

https://claude.ai/code/session_017o9EnCzHyTdYKdcXshMyMZ

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Aug 25, 2026
Each pytest-of-<user>/pytest-N/ session dir now contains a `.origin`
file next to `.lock`, recording rootpath, pytest version, PID, and
hostname. External cleanup tooling (workstation janitors, CI cleanup
steps, editor temp sweeps) can attribute a session dir to its project
by reading a file, instead of walking /proc or lsof.

Writing is best-effort: any OSError during the write is swallowed so
a read-only mount, permissions error, or full disk never breaks a
test run.

Refs pytest-dev#14935.
@jawauntb
jawauntb force-pushed the jb/tmpdir-origin-sidecar branch from bd5af13 to ce60cd9 Compare August 25, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant